﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Markup;
using System.Windows.Controls.Primitives;
using System.Text.RegularExpressions;

namespace SmallProgram.Controls
{
    public delegate void SetCaret(int line, int column);

    public partial class ErrorListControl : ListView
    {
        static ErrorListControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ErrorListControl), new FrameworkPropertyMetadata(typeof(ErrorListControl)));
        }

        public ErrorListControl()
        {
            base.ItemContainerGenerator.ItemsChanged += new ItemsChangedEventHandler(this.OnItemsChanged);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            var closeButton = GetTemplateChild("CloseButton") as Hyperlink;
            closeButton.Click += new RoutedEventHandler(OnCloseClick);
        }

        private void DoubleAnimateHeight(double height)
        {
            DoubleAnimation animation = new DoubleAnimation(height, new Duration(TimeSpan.FromMilliseconds(200.0)));
            base.BeginAnimation(FrameworkElement.HeightProperty, animation);
        }        

        private void OnCloseClick(object sender, RoutedEventArgs e)
        {
            this.DoubleAnimateHeight(0.0);
        }

        private void OnItemsChanged(object sender, ItemsChangedEventArgs e)
        {
            if (e.ItemCount == 0)
            {
                this.DoubleAnimateHeight(0.0);
            }
            else
            {
                this.DoubleAnimateHeight(120.0);
            }
        }

        public SetCaret SetCaret;

        private void MoveToCurrentError()
        {
            if (base.SelectedItem != null)
            {
                string selectedItem = base.SelectedItem as string;
                if (selectedItem != null)
                {
                    Match match = new Regex("([0-9]*),([0-9]*): w*").Match(selectedItem);
                    if (match.Success)
                    {
                        int line = int.Parse(match.Groups[1].Value);
                        int column = int.Parse(match.Groups[2].Value);
                        if (SetCaret != null)
                        {
                            SetCaret(line, column);
                        }
                    }
                }
            }
        }

        protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
        {
            this.MoveToCurrentError();
            base.OnMouseDoubleClick(e);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                this.MoveToCurrentError();
                e.Handled = true;
            }
            base.OnKeyDown(e);
        }
    }
}
